import {Clazz} from "./Clazz";
import {MMDAnimationHelper} from 'three/examples/jsm/animation/MMDAnimationHelper.js';
import {OutlineEffect} from 'three/examples/jsm/effects/OutlineEffect.js';
import * as THREE from 'three';
import {mmdLoader} from "../core/Resource";
import * as CANNON from 'cannon-es'

class SoundGirl extends Clazz {
    constructor() {
        super();
        const _this = this;
        const modelFile = '../js/components/images/soundgirl/miku/miku_v2.pmd';
        const vmdFiles = ['../js/components/images/soundgirl/vmds/wavefile_v2.vmd'];
        this.helper = new MMDAnimationHelper({
            afterglow: 2.0
        });
        this.size = new THREE.Vector3(20, 100, 10);
        this.position = new THREE.Vector3(0, 50, 0);
        this.effect = new OutlineEffect(renderer);
        this.quaternion = new THREE.Vector4();

        this.model = null;
        mmdLoader.loadWithAnimation(modelFile, vmdFiles, function (mmd) {
            _this.model = mmd.mesh;
            _this.model.position.set(_this.position.x, _this.position.y, _this.position.z);
            _this.model.quaternion.copy(_this.quaternion);
            _this.quaternion = _this.model.quaternion;
            _this.model.scale.set(5, 5, 5);
            _this.phyMode = _this.createPhyMode();
            _this.model.computeBoundingBox()
            scene.add(_this.model);
            _this.helper.add(_this.model, {
                animation: mmd.animation,
                physics: true
            });

            _this.ikHelper = _this.helper.objects.get(_this.model).ikSolver.createHelper(0.25);
            _this.ikHelper.visible = false;
            scene.add(_this.ikHelper);
            _this.physicsHelper = _this.helper.objects.get(_this.model).physics.createHelper();
            _this.physicsHelper.visible = false;
            scene.add(_this.physicsHelper);

        }, _this.onProgress, null);

        this.createOutline();
        this.outline.scale.set(this.size.x, this.size.y, this.size.z);
        this.outline.position.set(this.position.x, this.position.y, this.position.z);
        this.outline.quaternion.copy(this.quaternion);
        scene.add(this.outline);
    }

    onProgress(xhr) {
        if (xhr.lengthComputable) {
            const percentComplete = xhr.loaded / xhr.total * 100;
            console.log(percentComplete + '% downloaded');
        }
    }


    getSize() {
        return this.size;
    }

    createPhyMode() {
        let sphereShape = new CANNON.Box(new CANNON.Vec3(this.size.x / 2, this.size.y / 2, this.size.z / 2));
        let sphereBody = new CANNON.Body({
            mass: 1,
            shape: sphereShape,
            position: new CANNON.Vec3(this.position.x, this.position.y, this.position.z),
        })
        sphereBody.shapeOffsets = [new CANNON.Vec3(0, 50, 0)];
        physicalWorld.addBody(sphereBody);
        return sphereBody;
    }

    update() {
        this.helper.update(deltaTime);
        if (null != this.phyMode) {
            this.position.copy(this.phyMode.position);
            this.quaternion.copy(this.phyMode.quaternion);
            this.outline.position.set(this.position.x, this.position.y, this.position.z);
            this.outline.quaternion.copy(this.quaternion)
            this.model.position.set(this.position.x, this.position.y, this.position.z);
            this.model.quaternion.copy(this.quaternion)
            if (null == this.aa) {
                this.aa = 1;
                //this.phyMode.velocity = new CANNON.Vec3(0, 20, 0);
            }
        }

        this.effect.render(scene, camera);
    }
}

export {SoundGirl}